﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Controls;

namespace MineSweeper.ValidationRules
{
   
    /// <summary>
    /// Validates that the given binding is actually a number
    /// </summary>
    public sealed class IsNumericValidator : ValidationRule
    {
        public override ValidationResult Validate(object value,
            CultureInfo cultureInfo)
        {
            bool isNumeric = IsNumeric(value);
            return new ValidationResult(isNumeric, isNumeric ? string.Empty : "Value must be numeric");
        }

        public static bool IsNumeric(object value)
        {
            value = value ?? 0;
            string stringValue = value.ToString();
            if (!string.IsNullOrEmpty(stringValue))
            {
                if (stringValue.Contains("%"))
                {
                    value = stringValue.Replace("%", "");
                }
            }
            return Microsoft.VisualBasic.Information.IsNumeric(value);
        }
    }
}